home *** CD-ROM | disk | FTP | other *** search
- /* ALAddr.M */
-
- OPT MODULE
- OPT EXPORT
-
- /* Parses the addr lines for name/addr */
-
- /*
- * Returns name,address
- *
- * Both must be allocated with String()
- *
- * Formulae is to find the first word with @ in it and extract it as the address, using the
- * remaining string as the name.
- */
- PROC parse_addr (str:PTR TO CHAR)
- DEF nam:PTR TO CHAR, addr:PTR TO CHAR, tmp:PTR TO CHAR, x, y, len
-
- len := StrLen (str)
- x := InStr (str, '@')
- IF (x > -1)
- /* Found it! */
- IF (IF (x > 0) THEN (invalid (str[x-1])) ELSE FALSE) OR (invalid (str[x+1]))
- /* Nope, just an @ by itself */
- nam,addr := parse_again (str, x)
- RETURN nam,addr
- ENDIF
-
- y := x
- WHILE ((x > 0) AND (invalid (str[x]) = FALSE))
- DEC x
- ENDWHILE
- IF (invalid (str[x])) THEN INC x
- /* Found the start, now find the end */
- WHILE (invalid (str[y]) = FALSE)
- INC y
- ENDWHILE
- IF (invalid (str[y])) THEN DEC y
-
- addr := String (y-x+1)
- StrCopy (addr, str+x, y-x+1)
- IF (len > (y-x+1))
- nam := String (len - y + x - 1)
- IF (x) THEN StrCopy (nam, str, x)
- IF (y < (len-1)) THEN StrAdd (nam, str+y+1)
- ELSE
- nam := String (1)
- ENDIF
- RETURN nam,addr
- ENDIF
-
- /* We have no @ (SMTP address) in the string. */
- x := InStr (str, '!')
- IF (x > -1)
- /* Found it! */
-
- IF (IF (x > 0) THEN (invalid (str[x-1])) ELSE FALSE) OR (invalid (str[x+1]))
- nam,addr := parse_again (str, x)
- RETURN nam,addr
- ENDIF
-
- y := x
- WHILE ((x > 0) AND (invalid (str[x]) = FALSE))
- DEC x
- ENDWHILE
- IF (invalid (str[x])) THEN INC x
- /* Found the start, now find the end */
- WHILE (invalid (str[y]) = FALSE)
- INC y
- ENDWHILE
- IF (invalid (str[y])) THEN DEC y
-
- addr := String (y-x+1)
- StrCopy (addr, str+x, y-x+1)
- IF (len > (y-x+1))
- nam := String (len - y + x - 1)
- IF (x) THEN StrCopy (nam, str, x)
- IF (y < (len-1)) THEN StrAdd (nam, str+y+1)
- ELSE
- nam := String (1)
- ENDIF
- RETURN nam,addr
- ENDIF
-
- /* We have no ! (UUCP address) in the string */
- /* Well, I know of no other easy way to extrapolate an address. */
- /* So just use the first word, and pray. (: */
- x := 0
- WHILE (invalid (str[x]))
- INC x
- ENDWHILE
- y := x
- WHILE (invalid (str[y]) = FALSE)
- INC y
- ENDWHILE
-
- IF (y > x)
- addr := String (y-x)
- StrCopy (addr, str+x, y-x)
- ELSE
- /* Well, can't find even a single word! */
- addr := String (1)
- ENDIF
- IF (y < len)
- nam := String (len - y)
- StrCopy (nam, str+y)
- ELSE
- nam := String (1)
- ENDIF
- ENDPROC nam,addr
-
-
- /*
- * Used only by parse_addr(). Sort of a private function.
- * I'm using a funciton because it's called from a few places.
- */
- PROC parse_again (str:PTR TO CHAR, x)
- DEF nam, addr, tmp
-
- INC x
- nam,addr := parse_addr (str + x)
- tmp := nam
- nam := String (StrLen (tmp) + x)
- StrCopy (nam, str, x)
- StrAdd (nam, tmp)
- DisposeLink (tmp)
- ENDPROC nam,addr
-
-
- /*
- * Checks to see if a character is one which can't appear in an address.
- */
- PROC invalid (ch)
- SELECT 256 OF ch
- CASE 0 TO 32, "<", ">", "(", ")", "[", "]", 127 TO 255
- RETURN TRUE
- DEFAULT
- RETURN FALSE
- ENDSELECT
- ENDPROC
-
-